package com.example.larouzey.broadnotifytest_201ljy;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by Larouzey on 2017/5/3.
 */

public class MyReceiver extends BroadcastReceiver {
    private Context context;
    private String str="";
    public static NotificationManager manager;
    public static int notifyID=1;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;
        str=intent.getStringExtra("para1");
        Toast.makeText(context,"接收到广播:"+str,Toast.LENGTH_SHORT).show();
        sendnotify();
    }

    private void sendnotify() {
        manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
        //对构造器进行配置
        //点击后进入哪个activity
        Intent intent = new Intent(context,NotifyActivity.class);
        intent.putExtra("para2",str);//传递参数
        PendingIntent pendingintent=PendingIntent.getActivity(context,notifyID,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        //配置标题、内容、图标
        builder.setContentTitle("通知标题")
                .setContentText("通知内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("通知")
                .setContentIntent(pendingintent)
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis());
        //发送通知
        manager.notify(notifyID,builder.build());
    }

}
